import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: '',
		token: ''
	},
	mutations: {
		login(state, provider) {

			state.hasLogin = true;
			state.userInfo = provider.user;
			state.token = provider.token;
			
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: provider
				
			}) 
			console.log(state.userInfo);
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			state.token = ''
			uni.removeStorage({  
                key: 'userInfo'  
            })
			//移除购物车角标
			uni.removeTabBarBadge({
				index:2
			})
		}
	},
	actions: {
	},
	getters:{
		// validateLogin(state) {
		// 	state.hasLogin = !!state.token;
			
		// 	return state.hasLogin;
		// }
		
		validateLogin: state => {
			state.hasLogin = !!state.token;
			return state.hasLogin;
		}
	}
})

export default store
